package com.example.aoptest.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.aoptest.common.annonation.HasAuth;
import com.example.aoptest.common.token.TokenUtil;
import com.example.aoptest.config.WebResultJson;
import com.example.aoptest.entity.User;
import com.example.aoptest.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;


    @PostMapping("/login")
    public WebResultJson login(@RequestBody User user){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",user.getUsername());
        queryWrapper.eq("password",user.getPassword());
        User user1 =  userService.getOne(queryWrapper);
        if(user1!=null){
            Long userId = user1.getId();
            Set<String> menuList= userService.getMenu(userId);
            user1.setMenu(menuList);
            String token = TokenUtil.generateToken(user1);
            return WebResultJson.ok(token);
        }
        return WebResultJson.fail();
    }

    @PostMapping("/test")
    @HasAuth(value = "menu1")
    public WebResultJson test(){
       return WebResultJson.ok("我真厉害！");
    }

}
